<template>
  <view class="login-container">
    <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <!-- 获取登录信息,这个跳转值和方法 -->
    <button class="btn-login" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
    <text class="tip">登录后尽享更多权益</text>
  </view>
</template>

<script>
  import {
    mapGetters,
    mapState,
    mapMutations
  } from 'vuex'
  import myLoginVue from './my-login.vue';
  export default {
    name: "my-login",
    data() {
      return {
        token:"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
      };
    },
    computed: {
      ...mapState('m_user', ['redirectInfo'])
    },
    methods: {
      ...mapMutations('m_user', ['updateUserInfo', 'updateToken','updateRedirectInfo']),
      // 获取用户登录授权信息
      getUserInfo(e) {
        // console.log(e);
        if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权!')
        this.updateUserInfo(e.detail.userInfo);
        this.getToken(e.detail);
      },
      async getToken(info) {
        // 获取code
        let [err, res] = await uni.login();
        if (err || res.errMsg !== "login:ok") return uni.$showMsg('登录失败!')
        // console.log(res);
        // console.log(info);
        // 获取准备数据
        const query = {
          code: res.code,
          encrypteData: info.encrypteData,
          iv: info.iv,
          rawData: info.rawData,
          signature: info.signature
        }
        // 获取token
        // 接口问题，老师设置了权限,机构内部才能用
        // const {
        //   data
        // } = await uni.$http.post('/api/public/v1/users/wxlogin', query);
        //  console.log(data);
        // 所以我们自己模拟了token
        this.updateToken(this.token);
        this.navigateBack()
      },
      navigateBack() {
        if (this.redirectInfo && this.redirectInfo.openType === 'switchTab') {
          uni.switchTab({
            url: this.redirectInfo.from,
           complete: () => {
             this.updateRedirectInfo(null)
           }
          });
        }
      }
      // 下面也可以
      // getToken(info){
      //   uni.login({
      //       // provider: 'weixin', //使用微信登录
      //       success: function (e) {
      //         console.log(e);
      //         console.log(info);
      //       },
      //       fail: function (e) {
      //         console.log(e);
      //       },
      //   })
      // }
    }
  }
</script>

<style lang="less">
  .login-container {
    position: relative;
    height: 750rpx;
    background-color: #f8f8f8;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    overflow: hidden;

    &::after {
      content: '';
      display: block;
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      height: 40px;
      background-color: white;
      border-radius: 100%;
      transform: translateY(50%);
    }

    .btn-login {
      width: 90%;
      font-weight: 530;
      color: #fff;
      border-radius: 100px;
      margin: 15px 0;
      background-color: #c00000;
    }

    .tip {
      font-size: 12px;
      color: gray;
    }
  }
</style>
